App({
  onLaunch() {
    // 初始化云开发
    wx.cloud.init({
      env: 'lalayingwu-9g5uuy8ada833ae1',
      traceUser: true
    })
    console.log('云环境初始化完成，当前环境ID:', 'lalayingwu-9g5uuy8ada833ae1')
  },

  globalData: {
    shopAddress: {
      name: "拉拉的鹦鹉小屋",
      address: "深圳雅宝拉拉的鹦鹉小屋",
      latitude: 22.600779,
      longitude: 114.060136,
      phone: "13800138000", // 可选添加联系电话
      qrCodeUrl: "cloud://lalayingwu-9g5uuy8ada833ae1.6c61-lalayingwu-9g5uuy8ada833ae1-1373617737/lalayingwu.jpg"
    }
  },

  // 记录访问日志
  async recordVisit(pagePath, parrotId = null) {
    try {
      const { OPENID } = await wx.cloud.callFunction({
        name: 'quickstartFunctions',
        data: { type: 'getOpenId' }
      })
      await wx.cloud.database().collection('visitor_records').add({
        data: {
          _openid: OPENID,
          pagePath,
          parrotId,
          visitTime: wx.cloud.database().serverDate(),
          deviceInfo: wx.getSystemInfoSync()
        }
      })
    } catch (err) {
      console.error('记录访问失败:', err)
    }
  },

  // 检查管理员权限
  async checkAdmin() {
    try {
      const { result: { openid } } = await wx.cloud.callFunction({
        name: 'quickstartFunctions',
        data: { type: 'getOpenId' }
      })
      const res = await wx.cloud.database()
        .collection('adminopenid')
        .where({ _openid: openid })
        .count()
      return res.total > 0
    } catch (err) {
      console.error('权限检查失败:', err)
      return false
    }
  }
})
